<template>
  <div class="login clearfix">
    <div class="login-wrap">
      <el-row justify="center" type="flex">
        <el-form ref="loginForm" :model="user" label-width="80px" status-icon>
          <h3>注册</h3>
          <hr>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
          </el-form-item>

          <el-form-item label="设置密码" prop="password">
            <el-input v-model="user.password" placeholder="请输入密码" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon type="primary" @click="doRegister()">注册账号</el-button>
          </el-form-item>
        </el-form>
      </el-row>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "login",
  data() {
    return {
      user: {
        username: "",
        password: ""
      },
    };
  },
  created() {
    // console.log($);
    // console.log("1111");
  },
  methods: {
    doRegister() {
      if (!this.user.username) {
        this.$message.error("请输入用户名！");
        return;
      } else if (!this.user.password) {
        this.$message.error("请输入密码！");
        return;
      } else {
        // this.$router.push({ path: "/" }); //无需向后台提交数据，方便前台调试
        axios
            .post("/user/register/", {
              userAccount: this.user.username,
              userPassword: this.user.password
            })
            .then(res => {
              // console.log("输出response.data", res.data);
              // console.log("输出response.data.status", res.data.status);
              if (res.data.code === 200) {
                this.$router.push({path: "/"});
              } else {
                alert("您输入的用户名已存在！");
              }
            });
      }
    },
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login {
  width: 100%;
  height: 730px;
  background: url("/public/bg.jpg") no-repeat;
  background-size: cover;
  overflow: hidden;
}

.login-wrap {
  background-color: rgba(255, 255, 255, 0.8);
  background-size: cover;
  width: 500px;
  height: 500px;
  /*  margin: 150px auto;*/
  margin-left: 140px;
  margin-top: 120px;
  overflow: hidden;
  padding-top: 20px;
  line-height: 40px;
}

h3 {
  color: #0babeab8;
  font-size: 24px;
}

hr {
  background-color: #444;
  margin: 20px auto;
}

.el-button {
  width: 80%;
  margin-left: -50px;
}
</style>